<?php
class Book_shelf_service extends CI_Model
{

    const MAX_SHELF_NUM = 50;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('book_shelf_model');
        $this->load->model('novel_model');
    }
    
    public function getShelfList($user_id, &$err)
    {
        if (empty($user_id))
        {
            $err = [
                'errorCode'     => PARAM_ERR,
                'errorMessage'  => '参数异常',
            ];
            return false;
        }
        $shelf_novel = $this->book_shelf_model->findAll([
            'fields'        => 'shelf_id, novel_id',
            'where'         => [
                'user_id'   => $user_id,
            ],
            'order_by'      => [
                'add_time'  => 'desc',
            ]
        ]);
        if (empty($shelf_novel))
        {
            return $shelf_novel;
        }
        $this->_fillShelfNovel($shelf_novel);
        return toCamelCaseKey($shelf_novel);
    }

    public function addNovelToShelf($novel_id, $user_id, &$err)
    {
        if (empty($novel_id) || empty($user_id))
        {
            $err = [
                'errorCode'     => PARAM_ERR,
                'errorMessage'  => '参数异常',
            ];
            return false;
        }
        if ($this->_checkExist($novel_id, $user_id))
        {
            $err = [
                'errorCode'     => PARAM_ERR,
                'errorMessage'  => '此书已加入书架',
            ];
            return false;
        }
        $user_shelf_num = $this->_countUserShelfNum($user_id);
        if ($user_shelf_num >= self::MAX_SHELF_NUM)
        {
            $err = [
                'errorCode'     => SYSTEM_ERR,
                'errorMessage'  => '书架书籍数量已达上限',
            ];
            return false;
        }
        $this->book_shelf_model->insert([
            'shelf_id'      => $this->book_shelf_model->getUuid(),
            'novel_id'      => $novel_id,
            'user_id'       => $user_id,
            'add_time'      => time(),
        ]);
        return true;
    }

    public function removeNovelFromShelf($novel_id, $user_id, &$err)
    {
        if (empty($novel_id) || empty($user_id))
        {
            $err = [
                'errorCode'     => PARAM_ERR,
                'errorMessage'  => '参数异常',
            ];
            return false;
        }
        $this->book_shelf_model->del([
            'where'     => [
                'novel_id'      => $novel_id,
                'user_id'       => $user_id,
            ]
        ]);
        return true;
    }
    
    private function _fillShelfNovel(&$lists)
    {
        $novel_ids = array_column($lists, 'novel_id');
        $novels = $this->novel_model->findAll([
            'fields'        => 'novel_id, novel_name, cover_url',
            'where_in'      => [
                'novel_id'  => $novel_ids,
            ]
        ]);
        $novels_map = array_column($novels, null, 'novel_id');
        array_walk($lists, function (&$row) use ($novels_map) {
            $novel_id = $row['novel_id'];
            $row['novel_name'] = $novels_map[$novel_id]['novel_name'] ?? '';
            $row['cover_url'] = $novels_map[$novel_id]['cover_url'] ?? '';
        });
    }

    private function _countUserShelfNum($user_id)
    {
        $count = $this->book_shelf_model->findOne([
            'fields'        => 'count(*) as cnt',
            'where'         => [
                'user_id'   => $user_id,
            ]
        ])['cnt'] ?? 0;
        return $count;
    }

    private function _checkExist($novel_id, $user_id)
    {
        $row = $this->book_shelf_model->findOne([
            'fields'            => 'shelf_id',
            'where'             => [
                'user_id'   => $user_id,
                'novel_id'  => $novel_id,
            ]
        ]);
        if (empty($row))
        {
            return false;
        }
        return true;
    }
}